<?php
require_once (__DIR__ . "/../inc/functions.php");
require_once (__DIR__ . "/Keywords.php");

class orders
{
	static function GetAll()
	{
		$conn = GetConnection();
		$results = $conn->query('SELECT * FROM orders');
		$error=$conn->error;
		return $results;
	}
	
	static function Get($id)
	{
		$conn = GetConnection();
		$results = $conn->query("SELECT * FROM orders WHERE id=$id");
		$row = $results->fetch_assoc();
		$error=$conn->error;
		return $row;
	}

	static function Insert($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = 	"INSERT INTO orders (userId,total,paymentId,dateShipped,paymentType,shipAddress) "
			.	"Values ($row2[userId],$row2[total],$row2[paymentId],'$row2[dateShipped]',$row2[paymentType],$row2[shipAddress]); ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Update($row)
	{
		//print_r ($row);	
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = 	"UPDATE orders "
			.	"Set userId=$row2[userId],total=$row2[total],paymentId=$row2[paymentId],dateShipped='$row2[dateShipped]',paymentType=$row2[paymentType],shipAddress=$row2[shipAddress] "
			.	"WHERE id=$row2[id] ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Delete($id)
	{
		$conn = GetConnection();
		$sql = 	"DELETE FROM orders "
			.	"WHERE id=$id ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;				
	}
	static function Validate($row)
	{
		$results = array();
		if(empty($row['dateShipped'])) $results['dateShipped'] = 'Date shipped is required';		
		if(empty($row['total'])) $results['total'] = 'Total is required';		
		
		return count($results) > 0 ? $results : true;
	}
	
}

?>
